#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <shadow.h>
#include <string.h>
#include <crypt.h>

/*
 * 真实用户登录
 * */
int main(int argc, char *argv[])
{

	if (argc < 2) {
		fprintf(stderr, "usage...\n");
		exit(1);
	}
	char *input_pwd;
	char *crypt_pwd;
	struct spwd *shadow_line;
	
	input_pwd = (char *)getpass("Please input password: ");
	shadow_line = getspnam(argv[1]);
	// 输出加密杂质
	puts(shadow_line->sp_pwdp);
	// 因为使用到库函数，需要对其进行链接，gcc shadow_login.c -lcrypt
	crypt_pwd = crypt(input_pwd, shadow_line->sp_pwdp);

	if (strcmp(shadow_line->sp_pwdp, crypt_pwd) == 0) {
		puts("login success...");
	} else {
		puts("login fail.");
	}

	exit(0);
}
